package com.css.bjdt_core.ryxx.service;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.css.bjdt_core.ryxx.entity.TDmZbEntity;

@Service
public class TDmZbService {
	@Autowired
	private Dao dao;

	public Map<String, TDmZbEntity> getAllRootNode(String tDmZb, String tDmZbHiber) {
		Sql sql = Sqls
				.create("select t1.DMCOD,t1.DMGRP,t1.DMCPT,t1.DMLEVCOD,t1.DMHND,t1.ISCANUSE,t2.DMPARENTCOD,t2.DMPARENTLEV from "
						+ tDmZb + " t1 left join " + tDmZbHiber + " t2 on t1.DMCOD = t2.DMCOD order by t1.dmcod asc");
		sql.setCallback(new SqlCallback() {
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				List<TDmZbEntity> list = new LinkedList<TDmZbEntity>();
				while (rs.next()) {
					TDmZbEntity tDmZbEntity = new TDmZbEntity();
					tDmZbEntity.setDmcod(rs.getString("DMCOD"));
					tDmZbEntity.setDmgrp(rs.getString("DMGRP"));
					tDmZbEntity.setDmcpt(rs.getString("DMCPT"));
					tDmZbEntity.setDmlevcod(rs.getString("DMLEVCOD"));
					tDmZbEntity.setDmhnd(rs.getString("DMHND"));
					tDmZbEntity.setIscanuse(rs.getString("ISCANUSE"));
					tDmZbEntity.setDmparentcod(rs.getString("DMPARENTCOD"));
					tDmZbEntity.setDmparentlev(rs.getString("DMPARENTLEV"));
					tDmZbEntity.setIsparent("1");
					list.add(tDmZbEntity);
				}
				return list;
			}
		});
		dao.execute(sql);
		List<TDmZbEntity> tDmZbEntityList = sql.getList(TDmZbEntity.class);

		Map<String, TDmZbEntity> tDmZbEntityMap = new LinkedHashMap<String, TDmZbEntity>();
		for (TDmZbEntity tDmZbEntity : tDmZbEntityList) {
			String key = "";
			if (null == tDmZbEntity.getDmparentcod() || tDmZbEntity.getDmparentcod().equals("")) {
				key = "root--" + tDmZbEntity.getDmcod();
			} else {
				key = tDmZbEntity.getDmparentcod() + "--" + tDmZbEntity.getDmcod();
			}
			tDmZbEntityMap.put(key, tDmZbEntity);
		}
		return tDmZbEntityMap;
	}

	public LinkedList<TDmZbEntity> getLikeByMap(Map<String, TDmZbEntity> map, String keyLike) {
		LinkedList<TDmZbEntity> list = new LinkedList<TDmZbEntity>();
		for (String key : map.keySet()) {
			if (key.indexOf(keyLike) > -1) {
				list.add(map.get(key));
			}
		}
		return list;
	}

	//没有Hiber表的关联直接取出字典表所有数据
	public List<TDmZbEntity> getAllNodes(String tDmZb) {
		Sql sql = Sqls.create("select t1.DMCOD,t1.DMGRP,t1.DMCPT,t1.DMLEVCOD,t1.DMHND,t1.ISCANUSE from " + tDmZb
				+ " t1 order by t1.dmcod asc");
		sql.setCallback(new SqlCallback() {
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				List<TDmZbEntity> list = new LinkedList<TDmZbEntity>();
				while (rs.next()) {
					TDmZbEntity tDmZbEntity = new TDmZbEntity();
					tDmZbEntity.setDmcod(rs.getString("DMCOD"));
					tDmZbEntity.setDmgrp(rs.getString("DMGRP"));
					tDmZbEntity.setDmcpt(rs.getString("DMCPT"));
					tDmZbEntity.setDmlevcod(rs.getString("DMLEVCOD"));
					tDmZbEntity.setDmhnd(rs.getString("DMHND"));
					tDmZbEntity.setIscanuse(rs.getString("ISCANUSE"));
					list.add(tDmZbEntity);
				}
				return list;
			}
		});
		dao.execute(sql);
		List<TDmZbEntity> tDmZbEntityList = sql.getList(TDmZbEntity.class);
		return tDmZbEntityList;
	}
}
